<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class mimin_jonu extends CI_Controller {
	var $title = "Jogja Nuws";
	var $cust_validation_msg = array(
		'alpha_dash'	=> 'Input harus berupa karakter, angka dan dash',
		'required'		=> '%s harus di isi',
		'matches'		=> '%s tidak cocok'
		);

	function __construct(){		
		parent::__construct();	
		$this->load->model(array('adm_model', 'user_model', 'post_model', 'page_model', 'kategori_model', 'settings_model', 'log_model', 'menu_model'));
	}

	public function index(){
		$this->_validate_cookie();

		$data['title'] = "Jogja Nuws";
		$data['desc'] = "Admin Dashboard";
		$data['page'] = "home";
		$data['page_title'] = "Dashboard";

		$data['log'] = $this->log_model->get_all_limit();

		//summary counter
		$data['tot_post'] = $this->post_model->count_all();
		$data['tot_published_post'] = $this->post_model->count_published();
		$data['tot_pending_post'] = $this->post_model->count_pending();
		$data['tot_blocked_post'] = $this->post_model->count_blocked();
		$data['tot_pages'] = $this->page_model->count_all();
		$data['tot_users'] = $this->user_model->count_all();
		$data['tot_verified_users'] = $this->user_model->count_verified();
		$data['tot_unverified_users'] = $this->user_model->count_unverified();
		$data['tot_blocked_users'] = $this->user_model->count_blocked();
		$data['tot_published_pages'] = $this->page_model->count_published();
		$data['tot_drafted_pages'] = $this->page_model->count_drafted();
		$data['tot_category'] = $this->kategori_model->count_all();

		$this->load->view('admin/index', $data);
	}
	public function login(){
		$data['title'] = $this->title;
		$data['desc'] = "Admin Login Area";

		$this->load->view('admin/login', $data);
	}
	public function adm_login_check(){
		$data['title'] = $this->title;
		$data['desc'] = "Admin Login Area";

		$input = $this->input->post();
		if($input['btn_adm_login'] != ""){
			$this->load->library('form_validation');
			$this->form_validation->set_error_delimiters('','');			
			$this->form_validation->set_message($this->cust_validation_msg);
			$rules = array(
				array(
					'field'	=> 'adm_usr',
					'label'	=> 'Username',
					'rules'	=> 'required|alpha_dash'
					),
				array(
					'field'	=> 'adm_pass',
					'label'	=> 'Password',
					'rules'	=> 'required'
					)
				);
			$this->form_validation->set_rules($rules);
			if($this->form_validation->run() == true){
				$login_data = $this->adm_model->login_check($input);
				if(count($login_data) > 0){
					$this->_make_cookie('c_adm_id', $login_data->jonu_admin_id, time()+3600);
					$this->_make_cookie('c_adm_usrname', $login_data->jonu_admin_username, time()+3600);
					$this->_make_cookie('c_adm_fullname', $login_data->jonu_admin_fullname, time()+3600);
					redirect(site_url("mimin_jonu"));
				}else{
					$this->session->set_flashdata('notif_status', '0');
					$this->session->set_flashdata('notif', 'Login Gagal');
					redirect(site_url("mimin_jonu/login"));
				}	
				
			}else{
				$this->load->view('admin/login', $data);
			}
		}else{
			redirect(site_url("mimin_jonu/login"));
		}
	}
	public function editprofile(){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Edit Profile Admin";
		$data['page'] = "profile_edit";
		$data['page_title'] = "Edit Profile";	
		$data['adm'] = $this->adm_model->getbyID(get_cookie('c_adm_id'));

		$this->load->view('admin/index', $data);
	}
	public function updateprofile($id){
		$input = $this->input->post();
		if($input['btn_edit_adm'] != ""){
			$data['title'] = $this->title;
			$data['desc'] = "Edit Profile Admin";
			$data['page'] = "profile_edit";

			$this->load->library('form_validation');
			$messages = array(
				'required'	=> '%s tidak boleh kosong',
				'matches'	=> '%s tidak cocok'
				);
			$this->form_validation->set_message($messages);
			$rules = array(
				array(
					'field'	=> 'adm_fullname',
					'label'	=> 'Nama Lengkap',
					'rules'	=> 'required'
					),
				array(
					'field'	=> 'adm_email',
					'label'	=> 'Email',
					'rules'	=> 'required'
					),
				array(
					'field'	=> 'adm_retype_pass',
					'label'	=> 'Password',
					'rules'	=> 'matches[adm_pass]'
					),
				array(
					'field'	=> 'old_pass',
					'label'	=> 'Password Lama',
					'rules'	=> 'required'
					)
				);			
			$this->form_validation->set_rules($rules);
			if($this->form_validation->run() == true){
				$do_update = $this->adm_model->update($input, $id);
				if($do_update){
					$this->_make_cookie('c_adm_fullname', $input['adm_fullname'], time()+3600);
					$this->session->set_flashdata('notif_status', '1');
					$this->session->set_flashdata('notif', 'Updating profile success');
				}else{
					$this->session->set_flashdata('notif_status', '0');
					$this->session->set_flashdata('notif', 'Updating profile fail');
				}
				redirect(site_url("mimin_jonu/editprofile"));	
			}else{
				$this->session->set_flashdata('notif_status', '0');
				$this->session->set_flashdata('notif', validation_errors());
				redirect(site_url("mimin_jonu/editprofile"));
			}
		}else{
			redirect(site_url("mimin_jonu"));
		}
	}

	# [start] user
	public function user(){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Users List";
		$data['page'] = "user";
		$data['page_title'] = "Users List";		
		$data['users'] = $this->user_model->get_verified();

		$this->load->view('admin/index', $data);
	}
	public function unverified_user(){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Unverfied User";
		$data['page'] = "user_unverified";
		$data['page_title'] = "Unverfied User";		
		$data['users'] = $this->user_model->get_unverified();

		$this->load->view('admin/index', $data);
	}
	public function blocked_user(){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Blocked User";
		$data['page'] = "user_blocked";
		$data['page_title'] = "Blocked User";		
		$data['users'] = $this->user_model->get_blocked();

		$this->load->view('admin/index', $data);
	}
	public function block_user($id){
		$this->_validate_cookie();
		$input = $this->input->post();
		if($input['adm_block_user'] != "" && $id != ""){
			if($this->user_model->block($id)){
				$this->session->set_flashdata('notif', 'Blocking user success');
				$this->session->set_flashdata('notif_status', '1');
			}else{
				$this->session->set_flashdata('notif', 'Blocking user fail');
				$this->session->set_flashdata('notif_status', '0');
			}
			redirect(site_url('mimin_jonu/user'));
		}else{
			redirect(site_url('mimin_jonu/user'));
		}
	}
	public function unblock_user($id){
		$this->_validate_cookie();
		$input = $this->input->post();
		if($input['adm_unblock_user'] != "" && $id != ""){
			if($this->user_model->unblock($id)){
				$this->session->set_flashdata('notif', 'Unblocking user success');
				$this->session->set_flashdata('notif_status', '1');
			}else{
				$this->session->set_flashdata('notif', 'Unblocking user fail');
				$this->session->set_flashdata('notif_status', '0');
			}
			redirect(site_url('mimin_jonu/user'));
		}else{
			redirect(site_url('mimin_jonu/user'));
		}
	}
	# [finish] user	

	# [start] post
	public function post($page_id=0){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Posts List";
		$data['page'] = "post";
		$data['page_title'] = "Published Post";		

		$page_query = $this->post_model->home_post();		
		$paginate_data = array(
			'page_id'		=> $page_id,
			'query'			=> $page_query,
			'base_url'		=> 'post',
			'uri_segment'	=> 3,
			'per_page'		=> $this->per_page_setter(),
			'num_links'		=> 3
			);
		$data['all_post'] = $this->post_paginator($paginate_data);

		$this->load->view('admin/index', $data);
	}
	public function edit_post($id){
		$this->_validate_cookie();

		$input = $this->input->post();
		if($id != ""){
			$data['title'] = $this->title;
			$data['desc'] = "Edit Post";
			$data['page'] = "post_edit";
			$data['page_title'] = "Edit Post";	
			$data['page'] = "post_edit";
			$data['category']	= $this->kategori_model->get_all();
			$data['post']	= $this->post_model->get_by_id($id);

			$this->load->view('admin/index', $data);
		}else{
			redirect(site_url('mimin_jonu/post'));
		}
	}
	public function update_post($id){
		$this->_validate_cookie();
		$input = $this->input->post();
		$image = $_FILES['photo'];
		if($input['adm_btn_post_edit'] != ""){
			$this->load->library('form_validation');
			$this->form_validation->set_error_delimiters('','');			
			$this->form_validation->set_message($this->cust_validation_msg);
			$rules = array(
				array(
					'field'	=> 'post_title',
					'label'	=> 'Judul Post',
					'rules'	=> 'required'
					),
				array(
					'field'	=> 'post_content',
					'label'	=> 'Konten',
					'rules'	=> 'required'
					)
				);
			$this->form_validation->set_rules($rules);
			if($this->form_validation->run() == true){
				#apakah file foto diganti?
				if($image['name'] != ""){ 
					$this->db->where('jonu_post_id', $id);
					$get_post = $this->db->get('jonu_post');
					$get_post_res = $get_post->result_array();
					$post_img_checker = $get_post_res[0]['jonu_post_image'];

					#apakah field jonu_post_image tidak kosong?
					if($post_img_checker != ""){
						$do_del_old_photo = unlink("./asset/img/posts-image/$input[curr_photo]");
						#apakah current photo berhasil dihapus?
						if($do_del_old_photo){
							$data = array('jonu_post_image' => '');
							$this->db->where('jonu_post_id', $id);
							#kosongkan field jonu_post_image
							$clear_img_dbrec = $this->db->update('jonu_post', $data); 	
						}
					}

					$this->load->library('upload');
					$new_file_name = 'postimage-'.date('Ymd-his').rand(5,20);
					$config['upload_path'] = './asset/img/posts-image/';
					$config['allowed_types'] = 'gif|jpg|jpeg|png';
					$config['max_size']	= '2000';
					$config['max_width']  = '1500';
					$config['file_name'] = $new_file_name;	


					$this->upload->initialize($config);
					if ($this->upload->do_upload('photo')){
						$upload_msg = $this->upload->data();
						$input['foto'] =$upload_msg['file_name'];
					}
				}

				$input['post_id'] = $id;

				$cat_counter = $this->db->count_all('jonu_category');
				$cat_str = "";
				for($i=1; $i<=$cat_counter; $i++){
					if(isset($input["cat_$i"])){
						$cat_str .= $input["cat_$i"]." ";
					}
				}
				$cat_str = substr($cat_str, 0, strlen($cat_str)-1);
				$input['category'] = explode(" ", $cat_str);		

				if($this->post_model->update_by_adm($input) == true){
					$this->session->set_flashdata('notif', 'Updating post success');
					$this->session->set_flashdata('notif_status', '1');
				}else{
					$this->session->set_flashdata('notif', 'Updating post fail');
					$this->session->set_flashdata('notif_status', '0');
				}
				redirect(site_url("mimin_jonu/edit_post/$id"));

			}else{
				$this->session->set_flashdata('notif', validation_errors());
				$this->session->set_flashdata('notif_status', '0');
				redirect(site_url("mimin_jonu/edit_post/$id"));
			}
		}else{
			redirect(site_url('mimin_jonu/post'));
		}	
	}
	public function delete_post($id){
		$this->_validate_cookie();
		$input = $this->input->post();
		if($id != ""){
			$data['title'] = $this->title;
			$data['desc'] = "Delete Post";
			$data['page'] = "post_delete";
			$data['page_title'] = "Delete Post";	
			$data['page'] = "post_delete";
			$data['category']	= $this->kategori_model->get_all();
			$data['post']	= $this->post_model->get_by_id($id);

			$this->load->view('admin/index', $data);
		}else{
			redirect(site_url('mimin_jonu/post'));
		}
	}
	public function blocked_post(){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Blocked Posts";
		$data['page'] = "post_blocked";
		$data['page_title'] = "Blocked Posts";		
		$data['all_post'] = $this->post_model->get_blocked();

		$this->load->view('admin/index', $data);
	}
	public function block_post($id){
		$this->_validate_cookie();		
		if($this->post_model->block($id)){
			$this->session->set_flashdata('notif', 'Blocking post success');
			$this->session->set_flashdata('notif_status', '1');
		}else{
			$this->session->set_flashdata('notif', 'Blocking post fail');
			$this->session->set_flashdata('notif_status', '0');
		}
		redirect(site_url('mimin_jonu/post'));
	}
	public function approve_post($id){
		$this->_validate_cookie();		
		$input = $this->input->post();
		if($input['adm_approve_post'] != ""){
			if($this->post_model->approve($id)){
				$this->session->set_flashdata('notif', 'Approving post success');
				$this->session->set_flashdata('notif_status', '1');
			}else{
				$this->session->set_flashdata('notif', 'Approving post fail');
				$this->session->set_flashdata('notif_status', '0');
			}
			redirect(site_url('mimin_jonu/post'));
		}else{
			redirect(site_url('mimin_jonu/post'));
		}
	}
	public function moderated_post(){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Pending Posts";
		$data['page'] = "post_pending";
		$data['page_title'] = "Pending Posts";		
		$data['all_post'] = $this->post_model->get_pending();

		$this->load->view('admin/index', $data);
	}
	public function pending_post($id){
		$this->_validate_cookie();		
		$input = $this->input->post();
		if($input['adm_pending_post'] != ""){
			if($this->post_model->pending($id)){
				$this->session->set_flashdata('notif', 'Delaying post success');
				$this->session->set_flashdata('notif_status', '1');
			}else{
				$this->session->set_flashdata('notif', 'Delaying post fail');
				$this->session->set_flashdata('notif_status', '0');
			}
			redirect(site_url('mimin_jonu/post'));
		}else{
			echo "xx";
		}
	}
	private function post_paginator($data){
		$this->load->library('pagination');
		$jml = $data['query'];
		//pengaturan pagination
		$config['base_url'] = site_url()."/mimin_jonu/".$data['base_url'];
		$config['uri_segment'] = $data['uri_segment'];
		$config['total_rows'] = count($jml);
		$config['per_page'] = $data['per_page'];
		$config['num_links'] = $data['num_links'];

		/*designing pagination*/
		$config['full_tag_open'] = "<span>";
		$config['full_tag_close'] = "</span>";
		$config['prev_link'] = '<i class="fa fa-backward"></i>';
		$config['prev_tag_open'] = "<span class='page_nav'>";
		$config['prev_tag_close'] = "</span>";
		$config['num_tag_open'] = "<span>";
		$config['num_tag_close'] = "</span>";
		$config['next_link'] = '<i class="fa fa-forward"></i>';
		$config['next_tag_open'] = "<span class='page_nav'>";
		$config['next_tag_close'] = "</span>";
		$config['cur_tag_open'] = "<span class='current'>";
		$config['cur_tag_close'] = "</span>";

		$this->pagination->initialize($config);
        //buat pagination
		$data['halaman'] = $this->pagination->create_links();
		$data['query'] = $this->post_model->home_post_paged($config['per_page'], $data['page_id']);
		$data['total'] = $config['total_rows'];

		return $data;
	}
	# [finish] post

	# [start] pages
	public function pages(){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Create Page";
		$data['page'] = "pages";
		$data['page_title'] = "Create Page";		

		$this->load->view('admin/index', $data);
	}
	public function store_page(){
		$input = $this->input->post();
		if($input['btn_add_page'] != ""){
			if($this->page_model->store($input)){
				$this->session->set_flashdata('notif_status', '1');
				$this->session->set_flashdata('notif', 'Creating page success');
			}else{
				$this->session->set_flashdata('notif_status', '1');
				$this->session->set_flashdata('notif', 'Creating page fail');
			}
			redirect(site_url('mimin_jonu/pages_published'));
		}else{
			redirect(site_url('mimin_jonu/pages'));
		}
	}
	public function pages_published($page_id=0){
		$this->_validate_cookie();
		$data['title'] = $this->title;
		$data['desc'] = "Published Page";
		$data['page'] = "pages_published";
		$data['page_title'] = "Published Page";		

		$page_query = $this->page_model->get_published();
		$paginate_data = array(
			'page_id'		=> $page_id,
			'query'			=> $page_query,
			'base_url'		=> 'pages_published',
			'uri_segment'	=> 3,
			'per_page'		=> $this->per_page_setter(),
			'num_links'		=> 3
			);
		$data['pages'] = $this->pages_paginator($paginate_data);

		$this->load->view('admin/index', $data);
	}
	public function pages_edit($id){
		$data['title'] = $this->title;
		$data['desc'] = "Edit Page";
		$data['page'] = "pages_edit";
		$data['page_title'] = "Edit Page";		
		$data['pages'] = $this->page_model->get_by_id($id);

		$this->load->view('admin/index', $data);	
	}
	public function update_page($id){
		$input = $this->input->post();
		if($input['btn_edit_page']){
			$input['page_id'] = $id;
			if($this->page_model->update($input)){
				$this->session->set_flashdata('notif_status', '1');
				$this->session->set_flashdata('notif', 'Updating page success');
			}else{
				$this->session->set_flashdata('notif_status', '0');
				$this->session->set_flashdata('notif', 'Updating page fail');
			}
			redirect(site_url("mimin_jonu/pages_edit/$id"));
		}else{
			redirect(site_url('mimin_jonu/pages'));
		}
	}
	public function pages_drafted(){
		$this->_validate_cookie();
		$data['title'] = $this->title;
		$data['desc'] = "Drafted Page";
		$data['page'] = "pages_drafted";
		$data['page_title'] = "Drafted Page";		
		$data['pages'] = $this->page_model->get_drafted();

		$this->load->view('admin/index', $data);
	}
	public function pages_delete($id){
		$data['title'] = $this->title;
		$data['desc'] = "Delete Page";
		$data['page'] = "pages_delete";
		$data['page_title'] = "Delete Page";		
		$data['pages'] = $this->page_model->get_by_id($id);

		$this->load->view('admin/index', $data);
	}
	public function destroy_page($id){
		$input = $this->input->post();
		if($input['btn_delete_page']){
			$input['page_id'] = $id;
			if($this->page_model->destroy($id)){
				$this->session->set_flashdata('notif_status', '1');
				$this->session->set_flashdata('notif', 'Deleting page success');
			}else{
				$this->session->set_flashdata('notif_status', '0');
				$this->session->set_flashdata('notif', 'Deleting page fail');
			}
			redirect(site_url("mimin_jonu/pages_published"));
		}else{
			redirect(site_url('mimin_jonu/pages'));
		}
	}

	private function pages_paginator($data){
		$this->load->library('pagination');
		$jml = $data['query'];
		//pengaturan pagination
		$config['base_url'] = site_url()."/mimin_jonu/".$data['base_url'];
		$config['uri_segment'] = $data['uri_segment'];
		$config['total_rows'] = count($jml);
		$config['per_page'] = $data['per_page'];
		$config['num_links'] = $data['num_links'];

		/*designing pagination*/
		$config['full_tag_open'] = "<span>";
		$config['full_tag_close'] = "</span>";
		$config['prev_link'] = '<i class="fa fa-backward"></i>';
		$config['prev_tag_open'] = "<span class='page_nav'>";
		$config['prev_tag_close'] = "</span>";
		$config['num_tag_open'] = "<span>";
		$config['num_tag_close'] = "</span>";
		$config['next_link'] = '<i class="fa fa-forward"></i>';
		$config['next_tag_open'] = "<span class='page_nav'>";
		$config['next_tag_close'] = "</span>";
		$config['cur_tag_open'] = "<span class='current'>";
		$config['cur_tag_close'] = "</span>";

		$this->pagination->initialize($config);
        //buat pagination
		$data['halaman'] = $this->pagination->create_links();
		$data['query'] = $this->page_model->published_paged($config['per_page'], $data['page_id']);
		$data['total'] = $config['total_rows'];

		return $data;
	}
	# [finish] pages

	# [start] category
	public function category($page_id=0){
		$this->_validate_cookie();		       

		$data['title'] = $this->title;
		$data['desc'] = "Categories List";
		$data['page'] = "category";
		$data['sub_page'] = "category_add";
		$data['page_title'] = "Categories";		
		$data['category'] = $this->kategori_model->get_all();

		$page_query = $this->kategori_model->get_all();
		$paginate_data = array(
			'page_id'		=> $page_id,
			'query'			=> $page_query,
			'base_url'		=> 'category',
			'func_model'	=> 'get_all',
			'uri_segment'	=> 3,
			'per_page'		=> $this->per_page_setter(),
			'num_links'		=> 3
			);
		$data['paginator'] = $this->category_paginator($paginate_data);
		$this->load->view('admin/index', $data);
	}	
	private function category_paginator($data){
		$this->load->library('pagination');
		$jml = $data['query'];
		//pengaturan pagination
		$config['base_url'] = site_url()."/mimin_jonu/".$data['base_url'];
		$config['uri_segment'] = $data['uri_segment'];
		$config['total_rows'] = count($jml);
		$config['per_page'] = $data['per_page'];
		$config['num_links'] = $data['num_links'];

		/*designing pagination*/
		$config['full_tag_open'] = "<span>";
		$config['full_tag_close'] = "</span>";
		$config['prev_link'] = '<i class="fa fa-backward"></i>';
		$config['prev_tag_open'] = "<span class='page_nav'>";
		$config['prev_tag_close'] = "</span>";
		$config['num_tag_open'] = "<span>";
		$config['num_tag_close'] = "</span>";
		$config['next_link'] = '<i class="fa fa-forward"></i>';
		$config['next_tag_open'] = "<span class='page_nav'>";
		$config['next_tag_close'] = "</span>";
		$config['cur_tag_open'] = "<span class='current'>";
		$config['cur_tag_close'] = "</span>";

		$this->pagination->initialize($config);
        //buat pagination
		$data['halaman'] = $this->pagination->create_links();
		$data['query'] = $this->kategori_model->all_paged($config['per_page'], $data['page_id']);
		$data['total'] = $config['total_rows'];

		return $data;
	}
	public function addcategory(){
		$this->_validate_cookie();		       

		$data['title'] = $this->title;
		$data['desc'] = "Add Category";
		$data['page'] = "category_add";
		$data['page_title'] = "Add Category";	

		$this->load->view('admin/index', $data);
	}
	public function storecategory(){
		$input = $this->input->post();
		if(isset($input['btn_add_cat'])){
			$this->load->library('form_validation');
			$messages = array(
				'required'	=> '%s tidak boleh kosong',
				'is_unique'	=> '%s sudah ada di database'
				);
			$this->form_validation->set_message($messages);
			$rules = array(
				array(
					'field'	=> 'cat_name',
					'label'	=> 'Nama Kategori',
					'rules'	=> 'required|is_unique[jonu_category.jonu_category_name]'
					)
				);
			$this->form_validation->set_rules($rules);
			$this->form_validation->set_error_delimiters('','');
			if($this->form_validation->run() == true){
				$do_insert = $this->kategori_model->store($input);
				if($do_insert){
					$this->session->set_flashdata('notif_status', '1');
					$this->session->set_flashdata('notif', 'Storing category success');
				}else{
					$this->session->set_flashdata('notif_status', '0');
					$this->session->set_flashdata('notif', 'Storing category fail');
				}
				redirect(site_url("mimin_jonu/category"));
			}else{
				$this->session->set_flashdata('notif_status', '0');
				$this->session->set_flashdata('notif', validation_errors());
				redirect(site_url("mimin_jonu/category"));
			}
		}else{
			redirect(site_url("mimin_jonu/category"));
		}
	}
	public function editcategory($id, $page_id=0){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Edit Category";
		$data['page'] = "category_edit";
		$data['page_title'] = "Edit Category";	
		$data['cat'] = $this->kategori_model->get_by_id($id);
		$data['category'] = $this->kategori_model->get_all();

		$this->load->view('admin/index', $data);
	}
	public function updatecategory($id){
		$input = $this->input->post();
		if($input['btn_edit_cat'] != ""){
			$this->load->library('form_validation');
			$messages = array(
				'required'	=> '%s tidak boleh kosong',
				'is_unique'	=> '%s sudah ada di database'
				);
			$this->form_validation->set_message($messages);
			$rules = array(
				array(
					'field'	=> 'cat_name',
					'label'	=> 'Nama Kategori',
					'rules'	=> 'required'
					),
				array(
					'field'	=> 'cat_alias',
					'label'	=> 'Alias',
					'rules'	=> 'required'
					),
				array(
					'field'	=> 'cat_hastag',
					'label'	=> 'Hastag',
					'rules'	=> 'required'
					),
				array(
					'field'	=> 'cat_desc',
					'label'	=> 'Deskripsi',
					'rules'	=> 'required'
					)
				);
			$this->form_validation->set_rules($rules);
			$this->form_validation->set_error_delimiters('','');
			if($this->form_validation->run() == true){
				$do_update = $this->kategori_model->update($input, $id);
				if($do_update){
					$this->session->set_flashdata('notif_status', '1');
					$this->session->set_flashdata('notif', 'Updating category success');

				}else{
					$this->session->set_flashdata('notif_status', '1');
					$this->session->set_flashdata('notif', 'Updating category fail');
				}
				redirect(site_url("mimin_jonu/editcategory/$id"));
			}else{
				$this->session->set_flashdata('notif_status', '0');
				$this->session->set_flashdata('notif', validation_errors());
				redirect(site_url("mimin_jonu/editcategory/$id"));
			}
		}else{
			redirect(site_url("mimin_jonu/category"));
		}
	}
	public function deletecategory($id){
		$this->_validate_cookie();

		$data['title'] = $this->title;
		$data['desc'] = "Delete Category";
		$data['page'] = "category_delete";
		$data['page_title'] = "Delete Category";
		$data['cat'] = $this->kategori_model->get_by_id($id);
		$data['category'] = $this->kategori_model->get_all();

		$this->load->view('admin/index', $data);
	}
	public function destroycategory($id){
		$input = $this->input->post();
		$label = $input['cat_name'];
		if(isset($input['btn_delete_cat'])){
			$do_delete = $this->kategori_model->destroy($id);
			if($do_delete){
				$this->session->set_flashdata('notif_status', '1');
				$this->session->set_flashdata('notif', "Deleting category success");
			}else{
				$this->session->set_flashdata('notif_status', '0');
				$this->session->set_flashdata('notif', "Deleting category fail");
			}
			redirect(site_url("mimin_jonu/category"));
		}else{
			redirect(site_url("mimin_jonu/category"));
		}
	}
	# [end] category

	# [start] menus
	public function menu(){
		$this->_validate_cookie();		       

		$data['title'] = $this->title;
		$data['desc'] = "Menus List";
		$data['page'] = "menu";
		$data['sub_page'] = "menu_add";
		$data['page_title'] = "Menus List";		
		$data['menu'] = $this->menu_model->get_all();

		$this->load->view('admin/index', $data);
	}
	public function store_menu(){
		$this->_validate_cookie();
		$input = $this->input->post();

		if($input['btn_add_menu'] != ""){
			if($this->menu_model->store($input)){
				$this->session->set_flashdata('notif_status', '1');
				$this->session->set_flashdata('notif', 'Storing menu success');
			}else{
				$this->session->set_flashdata('notif_status', '0');
				$this->session->set_flashdata('notif', 'Storing menu fail');
			}
			redirect('mimin_jonu/menu');
		}else{
			redirect(site_url('mimin_jonu/menu'));
		}
	}
	public function edit_menu($id){
		$this->_validate_cookie();
		if($id != ""){
			$data['title'] = $this->title;
			$data['desc'] = "Edit Menu";
			$data['page'] = "menu";
			$data['sub_page'] = "menu_edit";
			$data['page_title'] = "Edit Menu";	
			$data['menu'] = $this->menu_model->get_all();
			$data['edit_menu'] = $this->menu_model->get_by_id($id);

			$this->load->view('admin/index', $data);
		}else{
			redirect(site_url('mimin_jonu/menu'));
		}
	}
	public function update_menu($id){
		$this->_validate_cookie();
		$input = $this->input->post();
		$input['menu_id'] = $id;

		if($input['btn_update_menu'] != "" && $id != ""){
			if($this->menu_model->update($input)){
				$this->session->set_flashdata('notif', 'Updating menu success');
				$this->session->set_flashdata('notif_status', '1');
			}else{
				$this->session->set_flashdata('notif', 'Updating menu success');
				$this->session->set_flashdata('notif_status', '1');
			}
			redirect(site_url("mimin_jonu/edit_menu/$id"));
		}else{
			redirect(site_url('mimin_jonu/menu'));
		}
	}
	public function delete_menu($id){
		$this->_validate_cookie();
		if($id != ""){
			$data['title'] = $this->title;
			$data['desc'] = "Edit Menu";
			$data['page'] = "menu_delete";
			$data['page_title'] = "Edit Menu";	
			$data['delete_menu'] = $this->menu_model->get_by_id($id);

			$this->load->view('admin/index', $data);
		}else{
			redirect(site_url('mimin_jonu/menu'));
		}
	}
	public function destroy_menu($id){
		$this->_validate_cookie();
		$input = $this->input->post();

		if($input['btn_destroy_menu'] != "" &&$id != ""){
			if($this->menu_model->destroy($id)){
				$this->session->set_flashdata('notif', 'Deleting menu success');
				$this->session->set_flashdata('notif_status', '1');
			}else{
				$this->session->set_flashdata('notif', 'Deleting menu fail');
				$this->session->set_flashdata('notif_status', '0');
			}
			redirect(site_url('mimin_jonu/menu'));
		}else{
			redirect(site_url('mimin_jonu/menu'));
		}
	}
	# [end] menus

	# [start] map view
	public function map_view(){
		$this->_validate_cookie();
		$data['title'] = $this->title;
		$data['desc'] = "Map View";
		$data['page'] = "map_view";
		$data['page_title'] = "Map View";	

		$this->load->view('admin/index', $data);
	}
	# [finish] map view

	# [start] ads config
	public function ad_add(){
		$this->_validate_cookie();
		$data['title'] = $this->title;
		$data['desc'] = "Create an Ad";
		$data['page'] = "ads_add";
		$data['page_title'] = "Create an Ad";	

		$this->load->view('admin/index', $data);
	}
	public function ads_config(){
		$this->_validate_cookie();
		$data['title'] = $this->title;
		$data['desc'] = "Ads Configuration";
		$data['page'] = "ads";
		$data['page_title'] = "Ads Configuration";	

		$this->load->view('admin/index', $data);
	}
	public function store_ad(){
		$this->_validate_cookie();
		$input = $this->input->post();
	}
	# [finish] ads config

	# [start] login data
	public function login_data($page_id=0){
		$this->_validate_cookie();
		$data['title'] = $this->title;
		$data['desc'] = "Login Data";
		$data['page'] = "login_data";
		$data['page_title'] = "Login Data";	
		//$data['login_data'] = $this->log_model->get_all();

		$page_query = $this->log_model->get_all();		
		$paginate_data = array(
			'page_id'		=> $page_id,
			'query'			=> $page_query,
			'base_url'		=> 'login_data',
			'uri_segment'	=> 3,
			'per_page'		=> 10,//$this->per_page_setter()
			'num_links'		=> 3
			);
		$data['login_data'] = $this->log_paginator($paginate_data);

		$this->load->view('admin/index', $data);
	}
	private function log_paginator($data){
		$this->load->library('pagination');
		$jml = $data['query'];
		//pengaturan pagination
		$config['base_url'] = site_url()."/mimin_jonu/".$data['base_url'];
		$config['uri_segment'] = $data['uri_segment'];
		$config['total_rows'] = count($jml);
		$config['per_page'] = $data['per_page'];
		$config['num_links'] = $data['num_links'];

		/*designing pagination*/
		$config['full_tag_open'] = "<span>";
		$config['full_tag_close'] = "</span>";
		$config['prev_link'] = '<i class="fa fa-backward"></i>';
		$config['prev_tag_open'] = "<span class='page_nav'>";
		$config['prev_tag_close'] = "</span>";
		$config['num_tag_open'] = "<span>";
		$config['num_tag_close'] = "</span>";
		$config['next_link'] = '<i class="fa fa-forward"></i>';
		$config['next_tag_open'] = "<span class='page_nav'>";
		$config['next_tag_close'] = "</span>";
		$config['cur_tag_open'] = "<span class='current'>";
		$config['cur_tag_close'] = "</span>";

		$this->pagination->initialize($config);
        //buat pagination
		$data['halaman'] = $this->pagination->create_links();
		$data['query'] = $this->log_model->get_all_paged($config['per_page'], $data['page_id']);
		$data['total'] = $config['total_rows'];

		return $data;
	}
	# [finish] login data

	# [start] settings
	public function settings(){
		$this->_validate_cookie();
		$data['title'] = $this->title;
		$data['desc'] = "Setting";
		$data['page'] = "settings";
		$data['page_title'] = "Settings";
		$data['settings'] = $this->settings_model->get_all();

		$this->load->view('admin/index', $data);
	}
	public function update_settings(){
		$input = $this->input->post();
		if($input['btn_update_settings']){
			if($this->settings_model->update($input)){
				$this->session->set_flashdata('notif', 'Updating setting success');
				$this->session->set_flashdata('notif_status', '1');
			}else{
				$this->session->set_flashdata('notif', 'Updating setting fail');
				$this->session->set_flashdata('notif_status', '0');
			}
			redirect('mimin_jonu/settings');
		}else{
			redirect('mimin_jonu/settings');
		}
	}
	private function per_page_setter(){
		$q_per_page_setting = $this->settings_model->get_all();
		foreach($q_per_page_setting as $q_per_page_setting_rec){
			if($q_per_page_setting_rec['jonu_setting_id'] == "adm_pagination"){
				$per_page_setter = $q_per_page_setting_rec['jonu_setting_value'];
			}
		}

		return $per_page_setter;
	}
	# [finish] settings

	public function logout(){
		$this->_delete_cookie("c_adm_id");
		$this->_delete_cookie("c_adm_usrname");
		$this->_delete_cookie("c_adm_fullname");
		$this->session->set_flashdata('notif', 'Anda telah logout');

		redirect(site_url("mimin_jonu/login"));
	}

	private function _validate_cookie(){
		if(get_cookie("c_adm_id") == "" && get_cookie("c_adm_usrname") == "" && get_cookie("c_adm_fullname") == ""){
			redirect(site_url("mimin_jonu/login"));
		}
	}

	private function _make_cookie($name, $value, $expire){
		set_cookie($name, $value, $expire);
	}

	private function _delete_cookie($name){
		delete_cookie($name);
	}	
}

/* End of file mimin_jonu.php */
/* Location: ./application/controllers/mimin_jonu.php */
